﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelephoneYiShou;


namespace Commodity
{
    public partial class Commodityadd : Skin_Mac
    {
        public Commodityadd()
        {
            InitializeComponent();
        }

        private void addbtn_Click(object sender, EventArgs e)
        {
            string nm = CommodityNametxt.Text;
            string tp = Typetxt.Text;
            string tpid=null;
            string inp = OrderGoodsPricetxt.Text;
            string outp = SellGoodsPricetxt.Text;
            string cl = cltxt.Text;
            string big = introducetxt.Text;
            string sup = Suppliernametxt.Text;
            string supid = null;
            if (string.IsNullOrEmpty(nm) ||
                string.IsNullOrEmpty(tp) || string.IsNullOrEmpty(inp) || string.IsNullOrEmpty(outp)
                || string.IsNullOrEmpty(cl) || string.IsNullOrEmpty(big)||string.IsNullOrEmpty(sup))
            {
                MessageBoxEx.Show("信息输入不完整！");
                return;//直接返回
            }
            //获取商品类型编号
            if (!string.IsNullOrEmpty(tp))
            {
                string tpsql = string.Format("select TypeID from CommodityType where TypeName='{0}'", tp);
                tpid= DBHelper.GetDataTable(tpsql).Rows[0][0].ToString();
            }
            //获取供应商
            if (!string.IsNullOrEmpty(sup))
            {
                string supsql = string.Format("select SupplierID from Supplier where SupplierName='{0}'", sup);
                supid = DBHelper.GetDataTable(supsql).Rows[0][0].ToString();
            }
            string sql = string.Format("insert into Commodity Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",nm, Convert.ToInt32(tpid), Convert.ToDecimal(inp), Convert.ToDecimal(outp), big, Convert.ToInt32(supid), cl);
            if (DBHelper.ExecuteNonQuery(sql))
            {
                MessageBoxEx.Show("商品添加成功！");
                string sqll = string.Format("select commodityid from commodity where commodityname= '{0}'", nm);
                DataTable dt = DBHelper.GetDataTable(sqll);
                string s = dt.Rows[0][0].ToString();
                //添加库存
                string ssql = string.Format("insert into stock values ('{0}','{1}')",s,0);
                DBHelper.ExecuteNonQuery(ssql);
            }
            else
            {
                MessageBoxEx.Show("添加失败,请联系管理员!");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“telephoneYiShouDataSet.Supplier”中。您可以根据需要移动或删除它。
            this.supplierTableAdapter.Fill(this.telephoneYiShouDataSet.Supplier);
            // TODO: 这行代码将数据加载到表“telephoneYiShouDataSet.Supplier”中。您可以根据需要移动或删除它。
            this.supplierTableAdapter.Fill(this.telephoneYiShouDataSet.Supplier);
            // TODO: 这行代码将数据加载到表“telephoneYiShouDataSet.CommodityType”中。您可以根据需要移动或删除它。
            this.commodityTypeTableAdapter.Fill(this.telephoneYiShouDataSet.CommodityType);

        }

        private void exitbtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
